/**
 * 
 */
package sk.seges.acris.mvp;

import java.util.List;

import com.google.gwt.activity.shared.AbstractActivity;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.user.client.ui.IsWidget;

/**
 * @author ladislav.gazo
 */
public abstract class AbstractDisplayAwareActivity<D extends IsWidget> extends AbstractActivity {
	protected final D display;
	private List<HandlerRegistration> handlerRegistrations = new java.util.ArrayList<HandlerRegistration>();

	public AbstractDisplayAwareActivity(D display) {
		super();
		this.display = display;
	}

	protected void registerHandler(HandlerRegistration handlerRegistration) {
		handlerRegistrations.add(handlerRegistration);
	}

	private void deregister() {
		for (HandlerRegistration registration : handlerRegistrations) {
			registration.removeHandler();
		}
		handlerRegistrations.clear();
	}

	@Override
	public void onCancel() {
		super.onCancel();
		deregister();
	}

	@Override
	public void onStop() {
		super.onStop();
		deregister();
	}
}
